package org.lsb.app_back.service;

import org.lsb.app_back.domain.AppCategory;
import org.lsb.app_back.mapper.AppCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppCategoryService {
    @Autowired
    AppCategoryMapper appCategoryMapper;

    public AppCategory getTree() {

        List<AppCategory> list=   appCategoryMapper.selectAll();

        AppCategory tree = new AppCategory();

        for (AppCategory appCategory : list) {
            //入口
            if (appCategory.getParentid() == null) {
                //找孩子
              tree=  findChildren(appCategory,list);

            }
        }

     return tree;
    }

    private AppCategory findChildren(AppCategory appCategory, List<AppCategory> list) {
        //递归找孩子
        appCategory.setChildren(new ArrayList<>());

        for (AppCategory category : list) {
            if (category.getParentid()==appCategory.getId()) {
                //说明是下一级孩子
                appCategory.getChildren().add(category);
                findChildren(category,list);

            }
        }

        return appCategory;

    }
}
